﻿Imports System.Data.SqlClient

Public Class deckActions
    Inherits System.Web.UI.Page
    Dim objDB As New DBConnect
    Dim ds As New DataSet
    Dim dr As SqlDataReader

    Dim objDB1 As New DBConnect
    Dim ds1 As New DataSet
    Dim dr1 As SqlDataReader

    Dim card As New Card
    Shared deck As Deck = New Deck()
    Shared hand As New Deck
    Public ajaxGE As ajaxGameEngine

    Public Sub New(ajaxGEpassed As ajaxGameEngine, passedGameID As String)
        ajaxGE = ajaxGEpassed
        If deck.hasCards Then
            deck.removeAllCards()
            hand.removeAllCards()
        End If

        Dim sqlCommand As String

        'sqlCommand = "SELECT Card.CardID, Card.CardCategoryID, Card.CardName, Card.CardInstruction, Card.CardDesc FROM Game, Game_Card, card, CardCategory WHERE Game.GameID = Game_Card.GameID And Game_Card.CardID = card.CardID And card.CardCategoryID = CardCategory.CardCategoryID And Game.GameID = '" & passedGameID & "'"
        sqlCommand = "SELECT DISTINCT Card.CardID, Card.CardName, Card.CardInstruction, Card.CardDesc, CardPlayAttribute_SystemAttribute.SystemID FROM(card, CardPlayAttribute_SystemAttribute) WHERE(card.CardID = CardPlayAttribute_SystemAttribute.CardID And CardPlayAttribute_SystemAttribute.SystemID = 1 And card.CardCategoryID = 1)"
        ds = objDB.getDataSet(sqlCommand)

        Dim numOfCards = ds.Tables(0).Rows.Count()



        For i As Integer = 0 To (numOfCards - 1)
            Dim instruction As String
            Dim description As String

            If IsDBNull(ds.Tables(0).Rows(i)("CardInstruction")) Then
                instruction = ""
            Else
                instruction = ds.Tables(0).Rows(i)("CardInstruction")
            End If

            If IsDBNull(ds.Tables(0).Rows(i)("CardDesc")) Then
                description = ""
            Else
                description = ds.Tables(0).Rows(i)("CardDesc")
            End If

            card = New Card(ds.Tables(0).Rows(i)("CardID"), ds.Tables(0).Rows(i)("CardCategoryID"), ds.Tables(0).Rows(i)("CardName"), instruction, description)
            deck.Add(card)
        Next
        'get systems in this game

    End Sub

    'Get the card from the Deck
    Public Function getCard(ByVal passedGameID As String)
        If deck.hasCards() Then
            card = deck.Deal()
            hand.Add(card)
        Else
            MsgBox("Sorry There Are No Cards to Deal")
        End If
        ajaxGE.Application("deckOfCards") = deck
        ajaxGE.Application("deckOfCommonCards") = hand
        getGameSystemID(passedGameID)
        Return card
    End Function

    'Shuffle the deck
    Public Sub deckShuffle()
        deck.Shuffle()
        ajaxGE.Application("deckOfCards") = deck
    End Sub

    'Get the card from the Deck
    Public Function getCardCommon()
        If hand.hasCards() Then
            card = hand.Deal()
            hand.Add(card)
        Else
            MsgBox("Sorry There Are No Cards to Deal")
        End If
        ajaxGE.Application("deckOfCommonCards") = hand
        Return card
    End Function

    'Shuffle the deck
    Public Sub deckShuffleCommon()
        deck.Shuffle()
        ajaxGE.Application("deckOfCards") = deck
    End Sub

    'Get a systemID for the card
    Private Sub getGameSystemID(matchIDpassed As String)
        Dim sqlCommand As String
        Dim systemIDSQL As ArrayList = New ArrayList()
        sqlCommand = "SELECT SystemID from Game_System WHERE GameID = '" & matchIDpassed & "'"

        ds1 = objDB1.getDataSet(sqlCommand)

        Dim numOfCards = ds1.Tables(0).Rows.Count()
        'If game exists then get a systemID
        If numOfCards > 0 Then
            For counter As Integer = 0 To numOfCards - 1
                Try
                    systemIDSQL.Add(ds1.Tables(0).Rows(counter)("SystemID").ToString)
                    card.cardSystemID = systemIDSQL
                Catch ex As Exception
                End Try
            Next
        End If

    End Sub
End Class